package com.lifeng.shop.lifengshop.controller;

import com.lifeng.shop.lifengshop.common.R;
import com.lifeng.shop.lifengshop.pojo.entry.Comment;
import com.lifeng.shop.lifengshop.pojo.vo.ProductCommentVO;
import com.lifeng.shop.lifengshop.servies.CommentServies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @author CLK
 */
@CrossOrigin
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentServies commentServies;

    @PostMapping("/selectByUidAndGid")
    public R selectComment(@RequestBody Map params){
        return new R(200,"查询评论成功",commentServies.selectByUidAndGid((Integer) params.get("uid"),(Integer) params.get("gid")));
    }
    @PostMapping("/selectByPrimaryKey")
    public R selectByPrimaryKey(@RequestBody Map params){
        return new R(200,"查询评论成功",commentServies.selectByPrimaryKey((Integer) params.get("cid")));
    }
    @PostMapping("/updateProductComment")
    public R updateProductComment(@RequestBody Map params){
        java.util.Date date = new java.util.Date();
        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp timeStamp = Timestamp.valueOf(simpleDate.format(date));

        Comment comment=new Comment((Integer) params.get("cid"),(Integer) params.get("gid"), (String) params.get("uid"),timeStamp, (String) params.get("comment_details"));
        return new R(200,"更新评论成功",commentServies.updateByPrimaryKey(comment));
    }
    @PostMapping("/insertProductComment")
    public R insertProductComment(@RequestBody Map params){
        java.util.Date date = new java.util.Date();
        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp timeStamp = Timestamp.valueOf(simpleDate.format(date));

        Comment comment=new Comment((Integer) params.get("gid"),(String)params.get("uid"),timeStamp, (String) params.get("comment_details"));
        return new R(200,"插入评论成功",commentServies.insertSelective(comment));
    }
    @PostMapping("/getProductComment")
    public R getProductComment(@RequestBody Map params){
        System.out.println(params.get("gid"));
        List<ProductCommentVO> comments=commentServies.selectByGid(Integer.valueOf((String) params.get("gid")));
        return new R(200,"查询商品评论成功",comments);
    }
}
